#include "stdafx.h"
#include "LitEditorSettingsWidget.h"

namespace Lit
{
	namespace Editor
	{
		SettingsTextWidget::~SettingsTextWidget(){}

		std::string SettingsTextWidget::getValue(){ return FROM_QT(mEdit->text()); }
		void SettingsTextWidget::textChanged( const QString &val )
		{
			emit( itemChanged( TO_QT(mName), val ) );
		}
		SettingsIntWidget::SettingsIntWidget( int min, int max, int value, QWidget*parent) {
			QHBoxLayout * lay = new QHBoxLayout(this);
			mSpin = new QSlider( this );
			mSpin->setRange( min, max );
			mSpin->setValue(value);
			mSpin->setSliderPosition( value );
			mSpin->setOrientation( Qt::Horizontal );
			mSpin->setMinimumSize( 80, 40 );
			mSpin->setTickPosition( QSlider::TicksAbove );
			mSpin->setTickInterval( 1 );
			lay->addWidget( mSpin );
			setLayout( lay );
				
			lay->setContentsMargins( 1,1,1,1 );
			mSpin->setContentsMargins( 1, 1, 1, 1 );
			connect( mSpin, SIGNAL(valueChanged(int)), this, SLOT(valueChanged( int )) );
		}
		SettingsIntWidget::~SettingsIntWidget(){}

		std::string SettingsIntWidget::getValue(){ return Ogre::StringConverter::toString(mSpin->value()); }
		void SettingsIntWidget::valueChanged( int val ){ emit(itemChanged(TO_QT(mName), TO_QT(Ogre::StringConverter::toString(mSpin->value())))); }

		SettingsDoubleWidget::SettingsDoubleWidget( double min, double max, double value, QWidget*parent) 
			: SettingsItemWidget( parent )
		{
			QHBoxLayout * lay = new QHBoxLayout(this);
			mSpin = new QDoubleSpinBox( this );
			mSpin->setMinimum( min );
			mSpin->setMaximum( max );
			mSpin->setValue( value );
			lay->addWidget( mSpin );
			setLayout( lay );
				
			lay->setContentsMargins( 1,1,1,1 );
			mSpin->setContentsMargins( 1, 1, 1, 1 );
			connect( mSpin, SIGNAL(valueChanged(double)), this, SLOT(valueChanged( double )) );
		}
		SettingsDoubleWidget::~SettingsDoubleWidget(){}

		std::string SettingsDoubleWidget::getValue(){ return Ogre::StringConverter::toString((float)mSpin->value()); }

		void SettingsDoubleWidget::valueChanged( double val )
		{
			emit(itemChanged( TO_QT(mName), TO_QT(Ogre::StringConverter::toString( (float)val )) ));
		}

		SettingsBoolWidget::SettingsBoolWidget(bool val, QWidget*parent) 
			: SettingsItemWidget( parent )
		{
			QHBoxLayout * lay = new QHBoxLayout(this);
			mBox = new QCheckBox( this );
			mBox->setChecked( val );
			mBox->setText( TO_QT(getValue()) );
			lay->addWidget( mBox );
			setLayout( lay );
				
			lay->setContentsMargins( 1,1,1,1 );
			mBox->setContentsMargins( 1, 1, 1, 1 );
			connect( mBox, SIGNAL(toggled(bool)), this, SLOT(boolChanged( bool )) );
		}
		SettingsBoolWidget::~SettingsBoolWidget(){}

		std::string SettingsBoolWidget::getValue(){
			return Ogre::StringConverter::toString( mBox->isChecked(), false );
		}

		void SettingsBoolWidget::boolChanged( bool ){
			mBox->setText( TO_QT(getValue()) );
			emit(itemChanged( TO_QT(mName), TO_QT(getValue()))); 
		}
		SettingsColorWidget::SettingsColorWidget(const Lit::Color &value, QWidget*parent) 
			: SettingsItemWidget( parent )
		{
			QHBoxLayout * lay = new QHBoxLayout(this);
			mLabel = new QLabel( "      ", this );
			setColor(value);
			lay->addWidget( mLabel );
			setLayout( lay );

			lay->setContentsMargins( 1,1,1,1 );
			mLabel->setContentsMargins( 1, 1, 1, 1 );
			//setMinimumHeight(20);
		}
		SettingsColorWidget::~SettingsColorWidget(){}

		void SettingsColorWidget::mousePressEvent( QMouseEvent * evt )
		{
			QColor cur(QColor::fromRgbF( mColor.r, mColor.g, mColor.b, mColor.a ));
			QColor col = QColorDialog::getColor( cur, this );
			setColor( Lit::Color( col.redF(), col.greenF(), col.blueF(), col.alphaF() ) );
			if( col != cur ) emit( itemChanged( TO_QT( mName ), TO_QT(Ogre::StringConverter::toString( mColor )) ) );
		}
		void SettingsColorWidget::setColor( const Lit::Color &value )
		{
			mColor = value;
			mLabel->setStyleSheet( "background-color: rgba("+TO_QT(Lit::String(255*value.r)(", ")(255*value.g)(", ")(255*value.b)(", ")(255*value.a).str())+"); border-color: black; border-style: solid; border-width: 1;" );
		}
		std::string SettingsColorWidget::getValue(){ return Ogre::StringConverter::toString( mColor ); }
	};
};